"""
doc
"""

import json
import logging
import requests

from django.core.cache import cache
from django.conf import settings
from django.views import View
from django.http import HttpRequest
from django.utils.decorators import method_decorator

from rest_framework.parsers import JSONParser

from app_common import (
    app_except,
    json_response_utf8_ok,
)

from . import wx_api, wx_pay, access_auth

logger = logging.getLogger("log")


@method_decorator(access_auth.owner_access, name="post")
class TcbTokenView(View):
    """doc"""

    def post(self, request: HttpRequest, *args, **kwargs):
        body = JSONParser().parse(request)

        params = {"env": settings.ENV_ID, "path": body["path"]}
        logger.info(params)
        r = requests.post(wx_api.tcb_token_url, json=params, timeout=10)
        d = json.loads(r.content)
        logger.info(d)
        return json_response_utf8_ok(d)


@method_decorator(access_auth.owner_access, name="post")
class TcbDownloadView(View):
    """doc"""

    def post(self, request: HttpRequest, *args, **kwargs):
        # https://developers.weixin.qq.com/miniprogram/dev/wxcloudrun/src/development/storage/service/download.html
        body = JSONParser().parse(request)

        params = {"env": settings.ENV_ID, "file_list": body["file_list"]}
        logger.info(params)
        r = requests.post(wx_api.tcb_download_url, json=params, timeout=10)
        d = json.loads(r.content)
        logger.info(d)

        return json_response_utf8_ok(d)
